<template>
  <view class="content b-t">
    <view class="list b-b" v-for="(item, index) in addressList" :key="index">
      <view class="wrapper" @click.stop="editAddress('edit', item, index)">
        <view class="address-box">
          <text style="width: 76rpx" v-if="item.default" class="tag">默认</text>
          <text class="address"
            >{{ item.province }}{{ item.city }}{{ item.district }}{{ item.address }} {{ item.houseNumber }}</text
          >
        </view>
        <view class="u-box">
          <text class="name">{{ item.name }}</text>
          <text class="mobile">{{ item.mobile }}</text>
        </view>
      </view>
      <view @click.stop="delAddress(index)">
        <uv-icon name="trash" color="red" size="28"></uv-icon>
      </view>
    </view>

    <button class="add-btn" @click="addAddress('add')">新增地址</button>
  </view>
</template>

<script>
import { searchUserInfoById, updateUserInfo } from "../../utils/api.js";
export default {
  data() {
    return {
      source: 0,
      addressList: [],
      nowBusinessInfo: {},
    };
  },
  onLoad(option) {
    console.log(option.source);
    this.source = option.source;
  },
  onShow() {
    this.nowBusinessInfo = uni.getStorageSync("nowBusinessInfo");
    this.getuserInfo();
  },
  methods: {
    // 获取信息地址
    getuserInfo() {
      let data = {
        userId: uni.getStorageSync("userInfo").id,
      };
      searchUserInfoById(data).then((res) => {
        if (res.code == 200) {
          if (
            res.body.receiveAddressJson != "" &&
            res.body.receiveAddressJson != null
          ) {
            this.addressList = JSON.parse(res.body.receiveAddressJson);
          }
        }
      });
    },
    //选择地址
    checkAddress(item) {
      if (this.source == 1) {
        //this.$api.prePage()获取上一页实例，在App.vue定义
        this.$api.prePage().addressData = item;
        uni.navigateBack();
      }
    },
    editAddress(type, item, index) {
      if (this.source == 0) {
        uni.navigateTo({
          url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(
            item
          )}&ind=${index}`,
        });
      } else {
        //this.$api.prePage()获取上一页实例，在App.vue定义
        uni.$emit("addressData", index);
        uni.navigateBack();
        return;
      }
    },
    addAddress(type, item, index) {
      uni.navigateTo({
        url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(
          item
        )}&ind=${index}`,
      });
    },
    //添加或修改成功之后回调
    delAddress(index) {
      //添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
      let that = this;
      uni.showModal({
        title: "删除地址",
        content: "地址删除后不可恢复，是否删除该地址？",
        success(res) {
          if (res.confirm) {
            that.addressList.splice(index, 1);
            let postData = {
              receiveAddressJson: JSON.stringify(that.addressList),
            };
            updateUserInfo(postData).then((res) => {
              if (res.code == 200) {
                uni.showToast({
                  title: "删除成功",
                });
                that.getuserInfo();
              } else {
                uni.showToast({
                  title: "删除失败",
                  duration: 1000,
                  icon: "error",
                });
              }
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
    },
  },
};
</script>

<style lang="scss">
page {
  padding-bottom: 120upx;
}
.content {
  position: relative;
}
.list {
  display: flex;
  align-items: center;
  padding: 20upx 30upx;
  background: #fff;
  position: relative;
}
.wrapper {
  display: flex;
  flex-direction: column;
  flex: 1;
}
.address-box {
  display: flex;
  align-items: center;
  .tag {
    font-size: 24upx;
    color:  #ff633d;
    margin-right: 10upx;
    background: #fffafb;
    border: 1px solid #ffb4c7;
    border-radius: 4upx;
    padding: 4upx 10upx;
    line-height: 1;
  }
  .address {
    font-size: 30upx;
    color: #303133;
  }
}
.u-box {
  font-size: 28upx;
  color:  #909399;
  margin-top: 16upx;
  .name {
    margin-right: 30upx;
  }
}
.icon-bianji {
  display: flex;
  align-items: center;
  height: 80upx;
  font-size: 40upx;
  color:  #909399;
  padding-left: 30upx;
}

.add-btn {
  position: fixed;
  left: 30upx;
  right: 30upx;
  bottom: 16upx;
  z-index: 95;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 690upx;
  height: 80upx;
  font-size: 32upx;
  color: #fff;
  background-color:  #ff633d;
  border-radius: 10upx;
  box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
}
</style>
